package com.xinggq.common.utils;

import org.springframework.core.env.Environment;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 统一的服务启动信息打印工具类
 * 用于在服务启动时打印统一格式的启动信息
 *
 * @author xinggq
 * @since 2025-10-20
 */
public class StartupInfoPrinter {

    /**
     * 打印网关服务启动信息
     *
     * @param env Spring环境对象
     */
    public static void printGatewayStartupInfo(Environment env) {
        String serverPort = env.getProperty("server.port", "8080");
        String contextPath = env.getProperty("server.servlet.context-path", "");
        String profile = env.getProperty("spring.profiles.active", "default");
        String appName = env.getProperty("spring.application.name", "Gateway Service");
        String version = env.getProperty("app.version", "1.0.0");
        String nacosAddr = env.getProperty("spring.cloud.nacos.discovery.server-addr", "unknown");
        
        printCommonHeader(appName);
        printGatewayAsciiArt();
        printCommonInfo(appName, version, profile, serverPort, contextPath, nacosAddr);
        
        // 访问地址
        System.out.println("\033[33m" + "🌐 访问地址:" + "\033[0m");
        System.out.println("   ├─ 本地访问: " + "\033[32m" + "http://localhost:" + serverPort + contextPath + "\033[0m");
        System.out.println("   ├─ 路由管理: " + "\033[32m" + "http://localhost:" + serverPort + contextPath + "/actuator/gateway/routes" + "\033[0m");
        System.out.println("   └─ 健康检查: " + "\033[32m" + "http://localhost:" + serverPort + contextPath + "/actuator/health" + "\033[0m");

        System.out.println();
        
        // 有趣的启动消息
        String[] messages = {
            "🚀 API网关服务已就绪，准备为您提供路由转发服务！",
            "✨ 所有路由检查完毕，网关运行状态良好！",
            "🎯 流量管控功能已激活，开始您的微服务之旅！",
            "💫 网关启动完成，让我们一起构建强大的分布式系统！",
            "🌟 负载均衡已配置，准备处理海量请求！"
        };
        
        printRandomMessage(messages);
        printFooter();
    }
    
    /**
     * 打印用户中心服务启动信息
     *
     * @param env Spring环境对象
     */
    public static void printUserCenterStartupInfo(Environment env) {
        String serverPort = env.getProperty("server.port", "8080");
        String contextPath = env.getProperty("server.servlet.context-path", "");
        String profile = env.getProperty("spring.profiles.active", "default");
        String appName = env.getProperty("spring.application.name", "User Center Service");
        String version = env.getProperty("app.version", "1.0.0");
        String nacosAddr = env.getProperty("spring.cloud.nacos.discovery.server-addr", "unknown");
        
        printCommonHeader(appName);
        printUserCenterAsciiArt();
        printCommonInfo(appName, version, profile, serverPort, contextPath, nacosAddr);
        
        // 访问地址
        System.out.println("\033[33m" + "🌐 访问地址:" + "\033[0m");
        System.out.println("   ├─ 本地访问: " + "\033[32m" + "http://localhost:" + serverPort + contextPath + "\033[0m");
        System.out.println("   ├─ API文档: " + "\033[32m" + "http://localhost:" + serverPort + contextPath + "/swagger-ui.html" + "\033[0m");

        System.out.println();
        
        // 有趣的启动消息
        String[] messages = {
            "🚀 用户中心服务已就绪，准备为您提供优质服务！",
            "✨ 所有系统检查完毕，服务运行状态良好！",
            "🎯 用户管理功能已激活，开始您的精彩之旅！",
            "💫 服务启动完成，让我们一起创造美好体验！"
        };
        
        printRandomMessage(messages);
        printFooter();
    }
    
    /**
     * 打印认证中心服务启动信息
     *
     * @param env Spring环境对象
     */
    public static void printAuthCenterStartupInfo(Environment env) {
        String serverPort = env.getProperty("server.port", "8080");
        String contextPath = env.getProperty("server.servlet.context-path", "");
        String profile = env.getProperty("spring.profiles.active", "default");
        String appName = env.getProperty("spring.application.name", "Auth Center Service");
        String version = env.getProperty("app.version", "1.0.0");
        String nacosAddr = env.getProperty("spring.cloud.nacos.discovery.server-addr", "unknown");
        
        printCommonHeader(appName);
        printAuthCenterAsciiArt();
        printCommonInfo(appName, version, profile, serverPort, contextPath, nacosAddr);
        
        // 访问地址
        System.out.println("\033[33m" + "🌐 访问地址:" + "\033[0m");
        System.out.println("   ├─ 本地访问: " + "\033[32m" + "http://localhost:" + serverPort + contextPath + "\033[0m");
        System.out.println("   ├─ API文档: " + "\033[32m" + "http://localhost:" + serverPort + contextPath + "/swagger-ui.html" + "\033[0m");
        System.out.println("   └─ Knife4j文档: " + "\033[32m" + "http://localhost:" + serverPort + contextPath + "/doc.html" + "\033[0m");
        
        System.out.println();
        
        // 有趣的启动消息
        String[] messages = {
            "🚀 认证中心服务已就绪，准备为您提供安全认证！",
            "✨ 所有安全检查完毕，认证系统运行状态良好！",
            "🎯 认证功能已激活，保障您的系统安全！",
            "💫 服务启动完成，为您的应用保驾护航！",
            "🔐 安全认证系统已启动，守护您的数据安全！"
        };
        
        printRandomMessage(messages);
        printFooter();
    }
    
    /**
     * 打印通用的头部信息
     */
    private static void printCommonHeader(String appName) {
        System.out.println();
        System.out.println("\033[36m" + "╔══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗" + "\033[0m");
    }
    
    /**
     * 打印网关服务的ASCII艺术
     */
    private static void printGatewayAsciiArt() {
        System.out.println("\033[36m" + 
            "║                                                                                      ║\n" +
            "║     ██████╗  █████╗ ████████╗███████╗██╗    ██╗ █████╗ ██╗   ██╗    ███████╗███████╗██████╗ ██╗   ██╗██╗ ██████╗███████╗ ║\n" +
            "║    ██╔════╝ ██╔══██╗╚══██╔══╝██╔════╝██║    ██║██╔══██╗╚██╗ ██╔╝    ██╔════╝██╔════╝██╔══██╗██║   ██║██║██╔════╝██╔════╝ ║\n" +
            "║    ██║  ███╗███████║   ██║   █████╗  ██║ █╗ ██║███████║ ╚████╔╝     ███████╗█████╗  ██████╔╝██║   ██║██║██║     █████╗   ║\n" +
            "║    ██║   ██║██╔══██║   ██║   ██╔══╝  ██║███╗██║██╔══██║  ╚██╔╝      ╚════██║██╔══╝  ██╔══██╗╚██╗ ██╔╝██║██║     ██╔══╝   ║\n" +
            "║    ╚██████╔╝██║  ██║   ██║   ███████╗╚███╔███╔╝██║  ██║   ██║       ███████║███████╗██║  ██║ ╚████╔╝ ██║╚██████╗███████╗ ║\n" +
            "║     ╚═════╝ ╚═╝  ╚═╝   ╚═╝   ╚══════╝ ╚══╝╚══╝ ╚═╝  ╚═╝   ╚═╝       ╚══════╝╚══════╝╚═╝  ╚═╝  ╚═══╝  ╚═╝ ╚═════╝╚══════╝ ║\n" +
            "║                                                                                      ║\n" +
            "\033[0m");
    }
    
    /**
     * 打印用户中心服务的ASCII艺术
     */
    private static void printUserCenterAsciiArt() {
        System.out.println("\033[36m" + 
            "║                                                                                             ║\n" +
            "║    ██╗   ██╗███████╗███████╗██████╗      ██████╗███████╗███╗   ██╗████████╗███████╗██████╗  ║\n" +
            "║    ██║   ██║██╔════╝██╔════╝██╔══██╗    ██╔════╝██╔════╝████╗  ██║╚══██╔══╝██╔════╝██╔══██╗ ║\n" +
            "║    ██║   ██║███████╗█████╗  ██████╔╝    ██║     █████╗  ██╔██╗ ██║   ██║   █████╗  ██████╔╝ ║\n" +
            "║    ██║   ██║╚════██║██╔══╝  ██╔══██╗    ██║     ██╔══╝  ██║╚██╗██║   ██║   ██╔══╝  ██╔══██╗ ║\n" +
            "║    ╚██████╔╝███████║███████╗██║  ██║    ╚██████╗███████╗██║ ╚████║   ██║   ███████╗██║  ██║ ║\n" +
            "║     ╚═════╝ ╚══════╝╚══════╝╚═╝  ╚═╝     ╚═════╝╚══════╝╚═╝  ╚═══╝   ╚═╝   ╚══════╝╚═╝  ╚═╝ ║\n" +
            "║                                                                                             ║\n" +
            "\033[0m");
    }
    
    /**
     * 打印认证中心服务的ASCII艺术
     */
    private static void printAuthCenterAsciiArt() {
        System.out.println("\033[36m" + 
            "║                                                                                               ║\n" +
            "║      █████╗ ██╗   ██╗████████╗██╗  ██╗     ██████╗███████╗███╗   ██╗████████╗███████╗██████╗  ║\n" +
            "║     ██╔══██╗██║   ██║╚══██╔══╝██║  ██║    ██╔════╝██╔════╝████╗  ██║╚══██╔══╝██╔════╝██╔══██╗ ║\n" +
            "║     ███████║██║   ██║   ██║   ███████║    ██║     █████╗  ██╔██╗ ██║   ██║   █████╗  ██████╔╝ ║\n" +
            "║     ██╔══██║██║   ██║   ██║   ██╔══██║    ██║     ██╔══╝  ██║╚██╗██║   ██║   ██╔══╝  ██╔══██╗ ║\n" +
            "║     ██║  ██║╚██████╔╝   ██║   ██║  ██║    ╚██████╗███████╗██║ ╚████║   ██║   ███████╗██║  ██║ ║\n" +
            "║     ╚═╝  ╚═╝ ╚═════╝    ╚═╝   ╚═╝  ╚═╝     ╚═════╝╚══════╝╚═╝  ╚═══╝   ╚═╝   ╚══════╝╚═╝  ╚═╝ ║\n" +
            "║                                                                                               ║\n" +
            "\033[0m");
    }
    
    /**
     * 打印通用的服务信息
     */
    private static void printCommonInfo(String appName, String version, String profile, String serverPort, String contextPath, String nacosAddr) {
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        
        System.out.println("\033[36m" + "╠══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣" + "\033[0m");
        System.out.println();
        System.out.println("\033[32m" + "🎉 " + appName + " 启动成功！" + "\033[0m");
        System.out.println();
        
        // 服务信息
        System.out.println("\033[33m" + "📋 服务信息:" + "\033[0m");
        System.out.println("   ├─ 服务名称: " + "\033[36m" + appName + "\033[0m");
        System.out.println("   ├─ 服务版本: " + "\033[36m" + version + "\033[0m");
        System.out.println("   ├─ 运行环境: " + "\033[36m" + profile + "\033[0m");
        System.out.println("   ├─ 服务端口: " + "\033[36m" + serverPort + "\033[0m");
        System.out.println("   ├─ 上下文路径: " + "\033[36m" + (contextPath.isEmpty() ? "/" : contextPath) + "\033[0m");
        System.out.println("   ├─ Nacos地址: " + "\033[36m" + nacosAddr + "\033[0m");
        System.out.println("   └─ 启动时间: " + "\033[36m" + currentTime + "\033[0m");
        
        System.out.println();
    }
    
    /**
     * 打印随机的启动消息
     */
    private static void printRandomMessage(String[] messages) {
        String randomMessage = messages[(int) (Math.random() * messages.length)];
        System.out.println("\033[35m" + randomMessage + "\033[0m");
    }
    
    /**
     * 打印页脚
     */
    private static void printFooter() {
        System.out.println();
        System.out.println("\033[36m" + "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + "\033[0m");
        System.out.println();
    }
}